import random
import time

"""
1 分钟数学运算
要求
    使用命令行在控制台中运行代码。
    它将运行 1 分钟。
    每次它都会显示 2 个随机数和随机算术运算，例如加、减、乘和除。如果运算是除法，则除数不能为 0。
    它会判断您的答案是否正确，然后显示下一个问题。
    当时间用完时，它会显示您回答了多少个问题，并显示总问题的正确率。
"""


# 获取除数,使得被除数被整除
def get_divisor(n):
    l = []  # 存放整除除数
    for i in range(1, n + 1):
        if n % i == 0:
            l.append(i)
    return random.choice(l)


if __name__ == '__main__':
    ops = ['+', '-', '*', '/']
    start_time = time.time()
    total = 0
    correct = 0
    questions = []
    while time.time() - start_time <= 60:
        a = random.randint(1, 99)
        op = random.choice(ops)
        if op == '/':
            b = get_divisor(a)
        else:
            b = random.randint(1, 99)
        a_op_b = f'{a}{op}{b}'
        c = int(eval(a_op_b))
        # 用户回答
        try:
            ans = int(input(f'{a_op_b} = '))
        except TypeError as e:
            ans = ''

        if time.time() - start_time <= 60:
            if c == ans:
                print(f'Correct! Time remain {int(60 - (time.time() - start_time))} seconds.')
                correct += 1
            else:
                print(f'Wrong answer! Time remain {int(60 - (time.time() - start_time))} seconds')
            total += 1
            questions.append(f'{a_op_b} = {ans}')
    print(f'{total} question and your correct rate is {(correct / total * 100):.2f}%')
    for q in questions:
        print(q)
